package com.java.dao;

import com.java.model.User;
import com.java.util.MD5Util;
import com.java.util.PropertiesUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {

	public User login(Connection con,User user) throws Exception{
		User resultUser=null;
		String sql="select * from t_user where userName=? and passWord=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, MD5Util.EncoderPwdByMd5(user.getPassWord()));
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			resultUser=new User();
			resultUser.setUserId(rs.getInt("userId"));
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassWord(rs.getString("passWord"));
			resultUser.setNickName(rs.getString("nickName"));
			resultUser.setImageName(PropertiesUtil.getValue("ImageFile")+rs.getString("imageName"));
			resultUser.setMood(rs.getString("mood"));
		}
		return resultUser;
	}

	//修改个人信息的方法
	public int userUpdate(Connection con,User user) throws Exception{
		String sql="update t_user set nickName=?,imageName=?,mood=? where userId=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1,user.getNickName());
		pstmt.setString(2,user.getImageName());
		pstmt.setString(3,user.getMood());
		pstmt.setInt(4,user.getUserId());
		return pstmt.executeUpdate();
	}
}
